package mbms.mobilebudget.income;

import java.util.Arrays;
import java.util.List;

import mbms.mobilebudget.R;
import mbms.mobilebudget.UserManager;
import mbms.mobilebudget.database.DatabaseHandler;
import mbms.mobilebudget.entity.BankAccount;
import mbms.mobilebudget.entity.CreditCard;
import mbms.mobilebudget.entity.User;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;

public class BankAccountCreditCardListActivity extends OrmLiteBaseActivity<DatabaseHandler> {
	 
		private static final String CREDIT_CARDS = "Credit Cards";
		private static final String BANK_ACCOUNTS = "Bank Accounts";
		private ExpandableListAdapter adapter;
	 
	    /** Called when the activity is first created. */
		@Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.bank_account_credit_card_selection);
	        List<String> groupList = Arrays.asList(new String[]{BANK_ACCOUNTS, CREDIT_CARDS});
	        
	        Intent intent = getIntent();
			User user = (User) intent.getSerializableExtra("authenticatedUser");
			Class clazz = (Class) intent.getSerializableExtra("clazz");
			
			UserManager userManager = new UserManager(getHelper(), this, user);
	        
	        List<BankAccount> bankAccountList = userManager.getBankAccountList();
	        List<CreditCard> creditCardList = userManager.getCreditCardList();
	        
		    adapter = new ExpandableListAdapter(this, bankAccountList, creditCardList, groupList);
		
		    ExpandableListView listView = (ExpandableListView) findViewById(R.id.bankAccountExpandableView);
	        
	        listView.setOnChildClickListener(new MyOnChildClickListener(clazz, this, bankAccountList, creditCardList, groupList));
	        
		    // Set this blank adapter to the list view
		    listView.setAdapter(adapter);
		}
	    
	    private class ExpandableListAdapter extends BaseExpandableListAdapter{
	    	 private Context context;
	    	 private List<BankAccount> bankAccountList;
	    	 private List<CreditCard> creditCardList;
	    	 private List<String> groupList;
	    	  
	    	 public ExpandableListAdapter(Context context, List<BankAccount> bankAccountList, List<CreditCard> creditCardList, List<String> groupList) {
				this.context = context;
				this.bankAccountList = bankAccountList;
				this.creditCardList = creditCardList;
				this.groupList = groupList;
	    	 }
	    	  
	    	 @Override
	    	 public Object getChild(int groupPosition, int childPosition) {
	    		 if(groupList.get(groupPosition).equals(BANK_ACCOUNTS)) {
	    			 return bankAccountList.get(childPosition);
	    		 } else {
	    			 return creditCardList.get(childPosition);
	    		 }
	    	 }
	    	 
	    	 @Override
	    	 public long getChildId(int groupPosition, int childPosition) {
	    		 return childPosition;
	    	 }
	    	 
	    	 @Override
	    	 public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
	    	   
	    	  Object object = getChild(groupPosition, childPosition);
	    	  if (view == null) {
	    		  LayoutInflater infalInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    		  
	    		 if(object instanceof BankAccount) {
	    			 view = infalInflater.inflate(R.layout.bank_account_row, null);
	    			 
	    			BankAccount bankAccount = (BankAccount)object;
	 	            if (bankAccount != null) {
 	                    TextView tt = (TextView) view.findViewById(R.id.toptext);
 	                    TextView bt = (TextView) view.findViewById(R.id.bottomtext);
 	                    if (tt != null) {
                          tt.setText("Name: "+bankAccount.getBankName());                            }
 	                    if(bt != null){
                          bt.setText("Curreny Type: "+ bankAccount.getCurrencyType().name());
 	                    }
	 	            }
	    		 } else {
	    			 view = infalInflater.inflate(R.layout.credit_card_row, null);
	    			 
	    			 CreditCard creditCard = (CreditCard)object;
		 	            if (creditCard != null) {
	 	                    TextView tt = (TextView) view.findViewById(R.id.toptext);
	 	                    TextView bt = (TextView) view.findViewById(R.id.bottomtext);
	 	                    if (tt != null) {
	                          tt.setText("Name: "+creditCard.getBankName());                            }
	 	                    if(bt != null){
	                          bt.setText("Curreny Type: "+ creditCard.getCurrencyType().name());
	 	                    }
		 	            }
	    		 }
	    	  }
	    	   
	    	  return view;
	    	 }
	    	 
	    	 @Override
	    	 public int getChildrenCount(int groupPosition) {
	    		 if(groupList.get(groupPosition).equals(BANK_ACCOUNTS)) {
	    			 return bankAccountList.size();
	    		 } else {
	    			 return creditCardList.size();
	    		 }
	    	 
	    	 }
	    	 
	    	 @Override
	    	 public Object getGroup(int groupPosition) {
	    	  return groupList.get(groupPosition);
	    	 }
	    	 
	    	 @Override
	    	 public int getGroupCount() {
	    	  return groupList.size();
	    	 }
	    	 
	    	 @Override
	    	 public long getGroupId(int groupPosition) {
	    	  return groupPosition;
	    	 }
	    	 
	    	 @Override
	    	 public View getGroupView(int groupPosition, boolean isLastChild, View view, ViewGroup parent) {
		    	  String headerInfo = (String) getGroup(groupPosition);
		    	  if (view == null) {
			    	  LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			    	  view = inf.inflate(R.layout.group_row, null);
		    	  }
		    	   
		    	  TextView heading = (TextView) view.findViewById(R.id.tvGroup);
		    	  heading.setText(headerInfo);
		    	   
		    	  return view;
	    	 }
	    	 
	    	 @Override
	    	 public boolean hasStableIds() {
	    		 return true;
	    	 }
	    	 
	    	 @Override
	    	 public boolean isChildSelectable(int groupPosition, int childPosition) {
	    		 return true;
	    	 }
	   	}
	    
	    private class MyOnChildClickListener implements OnChildClickListener {

	    	private List<BankAccount> bankAccountList;
			private List<CreditCard> creditCardList;
			private List<String> groupList;
			private Context context;
			private Class clazz;
			
			public MyOnChildClickListener(Class clazz, Context context, List<BankAccount> bankAccountList, List<CreditCard> creditCardList, List<String> groupList) {
				this.clazz = clazz;
				this.context = context;
				this.bankAccountList = bankAccountList;
				this.creditCardList = creditCardList;
				this.groupList = groupList;
			}
			@Override
			public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
				Intent intent = new Intent (context, clazz);
        		if(groupList.get(groupPosition).equals(BANK_ACCOUNTS)) {
        			intent.putExtra("passedBankAccount", bankAccountList.get(childPosition));
        		} else {
        			intent.putExtra("passedCreditCard", creditCardList.get(childPosition));
        		}
        		
        		startActivity(intent);
                return true;
			}
	    	
	    }
	}


